package cn.taokuaidi.wangwang.client.util;

/**
 * @author 嘉陵江里捉鱼 email:tang7259@163.com
 *@version 1.0
 *@time Nov 21, 2008 2:34:01 PM
 */
public class JavaScriptUtils {

    public static String javaScriptEscape(String input) {
        if (input == null) {
            return input;
        }

        StringBuffer filtered = new StringBuffer(input.length());
        char prevChar = '\u0000';
        char c;
        for (int i = 0; i < input.length(); i++) {
            c = input.charAt(i);
            if (c == '"') {
                filtered.append("\\\"");
            } else if (c == '\'') {
                filtered.append("\\'");
            } else if (c == '\\') {
                filtered.append("\\\\");
            } else if (c == '/') {
                filtered.append("\\/");
            } else if (c == '\t') {
                filtered.append("\\t");
            } else if (c == '\n') {
                if (prevChar != '\r') {
                    filtered.append("\\n");
                }
            } else if (c == '\r') {
                filtered.append("\\n");
            } else if (c == '\f') {
                filtered.append("\\f");
            } else if (c == '<') {
                filtered.append("\\'");
            } else if (c == '>') {
                filtered.append("\\'");
            } else if (c == ' ') {
            } else {
                filtered.append(c);
            }
            prevChar = c;

        }
        return filtered.toString();
    }
}
